package ace.module.mail.address.core.impl.manager.impl;

import ace.cmp.core.constants.CoreConstant;
import ace.cmp.data.spring.data.jdbc.querydsl.rsql.core.manager.spring.AbstractSpringDtoManager;
import ace.cmp.i18n.api.MessageSourceAssertService;
import ace.cmp.spring.data.jdbc.querydsl.core.util.DeleteFlagUtils;
import ace.module.account.api.enums.IamAccountStatusEnum;
import ace.module.mail.address.api.model.dto.MadApplicationDto;
import ace.module.mail.address.core.impl.dao.entity.MadApplication;
import ace.module.mail.address.core.impl.dao.repository.MadApplicationRepository;
import ace.module.mail.address.core.impl.manager.MadApplicationManager;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

/**
 * 邮寄地址应用 spring data jdbc querydsl rsql manager implement.
 *
 * @author Caspar
 */
@Slf4j
@AllArgsConstructor
@Component
public class MadApplicationManagerImpl
    extends AbstractSpringDtoManager<MadApplication, Long, MadApplicationDto>
    implements MadApplicationManager {

  private final MadApplicationRepository madApplicationRepository;
  private final MessageSourceAssertService assertService;

  @Override
  public MadApplication getByOutTypeAndOutId(String outType, String outId) {
    return madApplicationRepository.getByOutTypeAndOutIdAndDeleteFlag(outType, outId,
        CoreConstant.DELETE_FLAG_NOT);
  }

  @Override
  public MadApplication getAndCheckByOutTypeAndOutId(String outType, String outId) {
    MadApplication madApplication = madApplicationRepository.getByOutTypeAndOutIdAndDeleteFlag(
        outType, outId,
        CoreConstant.DELETE_FLAG_NOT);
    this.checkAll(madApplication);
    return madApplication;
  }

  private void checkAll(MadApplication madApplication) {
    assertService.isNull(madApplication, MadApplication.class.getName() + ".error-not-null",
        "数据不存在");

    assertService.isTrue(
        DeleteFlagUtils.isDelete(madApplication),
        MadApplication.class.getName() + ".error-not-null",
        "数据不存在");

    assertService.isTrue(
        !IamAccountStatusEnum.NORMAL.getCode().equals(madApplication.getStatus()),
        MadApplication.class.getName() + ".error-not-null",
        "账户已经锁定");
  }
}
